package com.tools.service.workflow.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tools.mapper.workflow.FlowNodeMapper;
import com.tools.pojo.workflow.bo.FlowNode;
import com.tools.service.workflow.FlowNodeService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FlowNodeServiceImpl extends ServiceImpl<FlowNodeMapper, FlowNode> implements FlowNodeService {

    /**
     * 根据流程定义ID获取所有节点
     */
    @Override
    public List<FlowNode> getNodesByFlowDefinitionId(Long flowDefinitionId) {
        return lambdaQuery()
                .eq(FlowNode::getFlowDefinitionId, flowDefinitionId)
                .orderByAsc(FlowNode::getExecuteOrder)
                .list();
    }

    /**
     * 根据流程定义ID和节点编码获取节点
     */
    @Override
    public FlowNode getNodeByCode(Long flowDefinitionId, String nodeCode) {
        return lambdaQuery()
                .eq(FlowNode::getFlowDefinitionId, flowDefinitionId)
                .eq(FlowNode::getNodeCode, nodeCode)
                .one();
    }

    /**
     * 获取流程的开始节点
     */
    @Override
    public FlowNode getStartNode(Long flowDefinitionId) {
        return lambdaQuery()
                .eq(FlowNode::getFlowDefinitionId, flowDefinitionId)
                .eq(FlowNode::getNodeType, "START")
                .one();
    }

    /**
     * 获取流程的结束节点
     */
    @Override
    public FlowNode getEndNode(Long flowDefinitionId) {
        return lambdaQuery()
                .eq(FlowNode::getFlowDefinitionId, flowDefinitionId)
                .eq(FlowNode::getNodeType, "END")
                .one();
    }

    /**
     * 根据节点类型获取节点
     */
    @Override
    public List<FlowNode> getNodesByType(Long flowDefinitionId, String nodeType) {
        return lambdaQuery()
                .eq(FlowNode::getFlowDefinitionId, flowDefinitionId)
                .eq(FlowNode::getNodeType, nodeType)
                .orderByAsc(FlowNode::getExecuteOrder)
                .list();
    }

}
